//
//  STCommodityDetailViewModel.swift
//  StarTimesON
//
//  Created by Toj on 8/19/22.
//  Copyright © 2022 StarTimes. All rights reserved.
//

import UIKit

enum DSSegmentLocation: String {
    case title
    case header
}

// MARK: - 初始化
class STCommodityDetailViewModel: NSObject {
    
    let ds_category = "MembershipCenter"
    
    weak var mobileViewModel: STCommodityMobileViewModel? = nil
    weak var tvViewModel: STCommodityTVViewModel? = nil
    weak var allViewModel: STCommodityAllViewModel? = nil
    
    // 如果不是空代表是主播推广进来的商品
    var utmString = ""
    
    private(set) var isOpenDVBService = false
    
    // ["MOBILE VIP"], ["MOBILE VIP", "ALL"], ["MOBILE VIP", "TV VIP", "ALL"]
    func setupTitles() -> [String] {
        if !utmString.isEmpty {
            return [CommoditySegmentType.mobile.rawValue]
        }
        
        let country = STUserModel.shared.areaDtoInfo?.country ?? "NONE"
        let isCountry = AppFunctionManager.TVOpenCountry[country] ?? ""
        isOpenDVBService = isCountry == "1"
        //print("-->toj: isCountry:\(isCountry), country: \(country)")
        if !isOpenDVBService {
            //print("-->toj: isCountry Open:\(isOpenDVBService)")
            isOpenDVBService = AppFunctionManager.isUsable(function: .FB_SELFSERVICE)
        }
        if !isOpenDVBService {
            return [CommoditySegmentType.mobile.rawValue,
                    CommoditySegmentType.all.rawValue]
        }
        
        return [CommoditySegmentType.mobile.rawValue,
                CommoditySegmentType.tv.rawValue,
                CommoditySegmentType.all.rawValue]
    }
}

extension STCommodityDetailViewModel {
    
    // 刷新OTT服务
    public func requestFetchOTTStatus(_ completion: ((_ isSuccess: Bool)->Void)? = nil) {
        OTTService.shared.fetchOTTStatus {
            completion?(true)
        } failure: { _ in
            completion?(false)
        }
    }
}

extension STCommodityDetailViewModel {

    // 上报页面时常
    func ds_pageexit(category: String, subpage: String) {
        let endTime = STCountDownTimer.shared.endTime(target: ds_category)
        let event = DSEvent(category: category, action: "pageexit", label: subpage, lt: .pv)
        DataStatistics.sendEvent(event: event, msg: ["deal_time":"\(endTime)"])
    }
    
    // 点击标签上报
    func ds_didTapSegementTag(subpage: String, segment location: DSSegmentLocation) {
        DataStatistics.sendEvent(event: DSEvent(category: ds_category, action: "show", label: subpage, value: "1"), msg: ["segment": location.rawValue])
    }
    
    // 退出商品页报数
    func ds_exitPopup(type: DSVAction.OTTMembership, value: Int) {
        let event = DSEvent(category: DSVCategory.membershipList.rawValue, action: type.rawValue, value: "\(value)")
        DataStatistics.sendEvent(event: event)
    }
}
